#!/bin/sh
#
# Copyright (c) 1990-1998 Enlighten Software Solutions, Inc.
#
# Example shell script to restart a process when Events notices
# the number of instances has fallen below the Low Threshold.
# Process tests are always performed every 30 seconds.
# This script will start one new copy of the process every 30
# seconds until the total count exceeds the low threshold
# (assuming the named process can be started, and they keep running).
#
# This script is a generic example only, it is not intended to handle
# all situations and may need some customization.  See below where
# the enivronment variables are set.
# 
# THIS IS UNSUPPORTED SOFTWARE, NO WARRANTY EXPRESS OR IMPLIED.
#
# Dec 1996, Ron Hitchens, Enlighten Software Solutions
#
#
# A command run by an AgentMon process instances event will be called like:
#	<commandname> "<processname> instances" <count> processes <type> <date>
#	
# Where
#	<commandname>	= The path of the command to run (this file).
#	"<processname> instances"
#			= The test name.  These two words will constitute
#			  one argument.  <processname> will be the name of
#			  the process you are monitoring.  The subname
#			  "instances" indicates you're measuring occurances,
#			  of the process.  This will be "size" for size
#			  tests, etc.  The "'s are not part of the argument.
#	<count>		= The value of the test (the current number of
#			  process with the name <processname>).
#	processes	= The literal string "processes".  Generically this
#			  is the test units name.
#	<type>		= The threshold type.  This will be "low" if the
#			  low threshold trips, "high" for the high threshold
#			  and "ok" when returning to normal from an alarm.
#	<date>		= The last argument is the time of the event, in
#			  unix date(1) format.  Note that there are spaces
#			  in imbedded in this argument.

type=$4		# set to event type passed in arg 4
set $1		# convert the compound argument to positional arguments
cmd=$1		# get the first piece <processname>

# Set this path to those you want to search for <processname>
# Commands are run by AgentMon, which is a daemon running as root
# with a minimal path.  Commands ARE NOT run in the GUI context.

PATH=/usr/local/bin:/usr/bin:/bin:/usr/openwin/bin:/usr/X11/bin:/usr/bin/X11
export PATH

# This will tell any X-Windows apps to run on the local host.  Even though
# the GUI is running under X, events commands are run by AgentMon which
# is not under the X environment.
# BEWARE: If you're creating process instance tests on a host other than
# the one running the GUI, this can cause windows to pop up on those
# remote displays, not where you're running the GUI.

DISPLAY=unix:0		# run on localhost
export DISPLAY

# if the alarm is a low threshold trigger, create another instance

if [ $type = "low" ] ; then
	exec $cmd
fi

# you can check for "ok", "high", etc and do other things if you like

exit 0

